<template>
  <el-form 
        :model="form" 
        ref="form"
        :rules="rules" 
        class="form">
        <el-form-item class="form-item" prop="username">
            <el-input
            v-model="form.username" 
            placeholder="用户名/手机">
            </el-input>
        </el-form-item>

        <el-form-item class="form-item" prop="password">
            <el-input 
            v-model="form.password"
            placeholder="密码" 
            type="password">
            </el-input>
        </el-form-item>

        <p class="form-text">
            <nuxt-link to="#">忘记密码</nuxt-link>
        </p>

        <el-button 
        class="submit"
        type="primary"
        @click="handleLoginSubmit">
            登录
        </el-button>
    </el-form>
</template>

<script>
export default {
    data(){
        return {
            form:{
                username:'',
                password:''
            },
            rules:{
                username:[
                    { required: true, message: '请输入正确的用户名', trigger: 'blur' },
                ],
                password:[
                    { required: true, message: '请输入正确的密码', trigger: 'blur' }
                ]
            },
            
        }
    },
    methods:{
        handleLoginSubmit(){//登录
            this.$refs.form.validate(async (valid)=>{
                if(valid){
                    try { 
                         await this.$store.dispatch('user/login',this.form)
                        // 存储到本地
             
                        this.$message({
                                message: "登录成功，正在跳转",
                                type: "success"
                            });
                        // 跳转到首页
                        setTimeout(() => {
                            // this.$router.replace("/");
                            // 登陆成功后返回上一个页面
                            this.$router.back()                            
                        }, 1000);
                    } catch (error) {
                        
                    }
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
 .form{
        padding:25px;
    }

    .form-item{
        margin-bottom:20px;
    }

    .form-text{
        font-size:12px;
        color:#409EFF;
        text-align: right;
        line-height: 1;
    }

    .submit{
        width:100%;
        margin-top:10px;
    }
</style>